package com.offer;


public class Test33 {

    public boolean verifyPostorder(int[] postorder) {
        return help(postorder, 0, postorder.length - 1);
    }

    private boolean help(int[] postorder, int left, int right) {
        if (left >= right) {
            return true;
        }

        int middleValue = postorder[right];

        int k = left;

        while (k < right && postorder[k] < middleValue) {
            k++;
        }

        for (int i = k; i < right; i++) {
            if (postorder[i] < middleValue) {
                return false;
            }
        }

        return help(postorder, left, k - 1) && help(postorder, k, right - 1);
    }


    public static void main(String[] args) {
        Test33 test33 = new Test33();
//        int[] array = new int[]{1, 3, 2, 6, 5};
        int[] array = new int[]{4, 8, 6, 12, 16, 14, 10};
//        int[] array = new int[]{4, 8};
        boolean result = test33.verifyPostorder(array);
        System.out.println(result);
    }

}
